

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class GraphMiningPresenter 
{
	/**
	 * 
	 * @param filepath
	 * @return An array with following allocation. 0 = minVertices, 1 = maxVertices, 2 = Average Vertices, 3 = minEdges, 4 = maxEdges, 5 = Average Edges
	 * 		
	 */
	public static int[] getInformation(String filepath)
	{
		int[] info = new int[6];
		info[0] = Integer.MAX_VALUE;
		info[1] = 0;
		info[3] = Integer.MAX_VALUE;
		info[4] = 0;
		 
		int allVertices = 0;
		int allEdges = 0;
		int allGraphs = 0;
		
		
		BufferedReader reader;
		try 
		{
			reader = new BufferedReader(new FileReader(filepath));
			
			String line;
			ArrayList<Integer> nodesInCurrentGraph = new ArrayList<Integer>();
			int edgesInCurrentGraph = 0;
			
			// skip first line
			reader.readLine();
	        while((line = reader.readLine()) != null ) 
	        {
	        	if (line.equals(""))
	        		continue;
	        	
	        	if (line.startsWith("Graphnumber"))
	        	{
	        		if (nodesInCurrentGraph.size() > info[1])
	        			info[1] = nodesInCurrentGraph.size();
	        		
	        		if (nodesInCurrentGraph.size() < info[0])
	        			info[0] = nodesInCurrentGraph.size();
	        		
	        		if (edgesInCurrentGraph > info[4])
	        			info[4] = edgesInCurrentGraph;
	        		
	        		if (edgesInCurrentGraph < info[3])
	        			info[3] = edgesInCurrentGraph;
	        		
	        		allVertices += nodesInCurrentGraph.size();
	        		allEdges += edgesInCurrentGraph;
	        		
	        		nodesInCurrentGraph.clear();
	        		edgesInCurrentGraph = 0;
	        		allGraphs++;
	        		continue;
	        	}
	        	edgesInCurrentGraph++;
	        
	        	String[] currentGraph = line.split(",");
	        	int firstNode = Integer.valueOf(currentGraph[0].trim());
	        	int secondNode = Integer.valueOf(currentGraph[1].trim());
	        	if (!nodesInCurrentGraph.contains(firstNode))
	        	{
	        		nodesInCurrentGraph.add(firstNode);
	        	}
	        	
	        	if (!nodesInCurrentGraph.contains(secondNode))
	        	{
	        		nodesInCurrentGraph.add(secondNode);
	        	}
	        }
	        reader.close();   
	        
	        
	        info[2] = allVertices / allGraphs;
	        info[5] = allEdges / allGraphs;
		} 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return info;
	}
}
